package com.aytbayev.resmi;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

public class ChangePassword extends Activity{
	
	private static final String USER_DETAILS = "UserPrefs";
	private boolean defaultPass = false;
	private SharedPreferences settings;
	
	@Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.change_password);         
        				
        Button ok = (Button) findViewById(R.id.ok_button);
        Button cancel = (Button) findViewById(R.id.cancel_button);        
        
        // OK button click
        ok.setOnClickListener( new OnClickListener() {
			public void onClick(View arg0) {
				String currPassword = ((EditText) findViewById(R.id.current_password_et)).getText().toString();
				String newPassword = ((EditText) findViewById(R.id.new_password_et)).getText().toString();
				String newPasswordConf = ((EditText) findViewById(R.id.confirm_new_password_et)).getText().toString();
				
				// check length
				if (newPassword.length() < 5) {
					GeneralHelper.showAlert(ChangePassword.this, ChangePassword.this.getString(R.string.new_password_length_5));
				} else if (newPassword.length() > 20){
					GeneralHelper.showAlert(ChangePassword.this, ChangePassword.this.getString(R.string.new_password_length_20));
				} else {				
					// check they are same
					if(newPassword.compareTo(newPasswordConf) != 0){
						GeneralHelper.showAlert(ChangePassword.this, ChangePassword.this.getString(R.string.confirm_new_password_wrong));
					} else {										
						String clientIdString = settings.getString("clientId", "0");
						int clientId = Integer.parseInt(clientIdString);
						
						if (clientId == 0) {
							// handle unknown error
							GeneralHelper.showAlert(ChangePassword.this, ChangePassword.this.getString(R.string.error));
						} else {							
							// actually change password							
							String result = DataManager.changePasswordMobile(clientId, Integer.toString(currPassword.hashCode()),
									Integer.toString(newPassword.hashCode()));
														
							if (result.equals("")) {
								Toast toast = Toast.makeText(getApplicationContext(), R.string.password_changed_sucessfully,
										Toast.LENGTH_LONG);
								toast.show();								
								if (defaultPass) {
									Intent myIntent = new Intent(ChangePassword.this, TabView.class);
									ChangePassword.this.startActivity(myIntent);
									
									SharedPreferences.Editor editor = settings.edit();
								    editor.putBoolean("defaultPass", false);
								    editor.commit();
								} 
								ChangePassword.this.finish();								
							} else if (result.equals(getString(R.string.passwords_dont_match))) {
								GeneralHelper.showAlert(ChangePassword.this, ChangePassword.this.getString(R.string.passwords_dont_match_alert));
							} else {
								Toast toast = Toast.makeText(getApplicationContext(), getString(R.string.password_changed_error),
										Toast.LENGTH_LONG);
								toast.show();											
								ChangePassword.this.finish();
								
							}						
						}
					}
				}				
			}        	
        });
        
        // Cancel button click
        cancel.setOnClickListener( new OnClickListener() {
			public void onClick(View arg0) {
				if (defaultPass) {
					GeneralHelper.showAlert(ChangePassword.this, ChangePassword.this.getString(R.string.default_password_change));
				} else {
					ChangePassword.this.finish();
				}
			}        	
        });
        
    }

	@Override
	public void onResume() { 
		super.onResume();
		settings = getSharedPreferences(USER_DETAILS, Context.MODE_PRIVATE);
		defaultPass = settings.getBoolean("defaultPass", false);
	}
}
